package com.huli.controller;

import com.huli.pojo.SysDepartment;
import com.huli.service.DepartmentService;
import com.huli.util.Result;
import com.huli.vo.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/departmentList")
    // 根据传入的departmentVo对象，调用departmentService的departmentList方法，返回DepartmentVo对象
    public Result departmentList(@RequestBody DepartmentVo departmentVo){
        DepartmentVo departmentVo1 = departmentService.departmentList(departmentVo);
        return Result.success(departmentVo1);
    }

    // 根据id删除科室
    @RequestMapping("/deleteDepartment")
    public Result deleteDepartment(Long id){
        try {
            departmentService.deleteDepartment(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }

    // 添加科室
    @RequestMapping("/addDepartment")
    public Result addDepartment(SysDepartment sysDepartment){
        try {
            departmentService.addDepartment(sysDepartment);
            return Result.success("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("添加失败");
        }
    }

    // 修改科室
    @RequestMapping("/updateDepartment")
    public Result updateDepartment(SysDepartment sysDepartment){
        try {
            departmentService.updateDepartment(sysDepartment);
            return Result.success("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("修改失败");
        }
    }

    // 查询科室名称
    @RequestMapping("/queryDepartmentName")
    public Result queryDepartmentName(){
        try {
            List<SysDepartment> departmentList = departmentService.queryDepartmentName();
            return Result.success(departmentList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    // 根据id查询科室
    @RequestMapping("/queryDepartmentById")
    public Result queryDepartmentById(Long id){
        try {
            SysDepartment sysDepartment = departmentService.queryDepartmentById(id);
            return Result.success(sysDepartment);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }
}